<template>
  <div class="page-search">
    <art-form v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h1 class="header">高级检索</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button @click="resetForm">
            <el-icon>
              <component :is="'Setting'"></component>
            </el-icon>
            <span>重置</span>
          </el-button>
          <el-button type="primary" @click="searchForm">
            <el-icon>
              <component :is="'Search'"></component>
            </el-icon>
            <span>搜索</span>
          </el-button>
        </div>
      </template>
    </art-form>
  </div>
</template>

<script lang='ts'>
import { defineComponent, ref, watch } from 'vue'

import ArtForm from '@/base-ui/form/index'

export default defineComponent({
  components: {
    ArtForm
  },

  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  emit: ['searchBtnClick', 'resetBtnClick'],
  setup(props, { emit }) {

    // const formData = ref({
    //   id: '',
    //   name: '',
    //   password: '',
    //   sports: ''
    // })
    // 1.获取formItems中的双向绑定key值
    const formItems = props.searchFormConfig.formItems || []
    const baseData: any = {}
    for(const item of formItems) {
      baseData[item.field] = ''
    }
    const formData = ref(baseData)

    // 2、重置表单数据
    const resetForm = () => {
      formData.value = baseData
      emit('resetBtnClick')
    }

    // 3、搜索获取数据
    const searchForm = () => {
      emit('searchBtnClick', formData.value)
    }

    return {
      formData,
      resetForm,
      searchForm
    }
  },
})
</script>

<style scoped lang="less">
.header {
  text-align: center;
  margin-top: 0;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>